<?php
namespace App\Http\Controllers\API\V1;

use App\Http\Controllers\Controller;
use Dingo\Api\Routing\Helpers;
use Request;
use PRedis;

class APIController extends Controller
{
    use Helpers;

    protected $request;
    protected $user; # 用户信息
    protected $filePath; # 公共文件路径
    protected $path; # 公共上传文件路径
    protected $domain;

    function __construct(Request $request)
    {
        $this->request = $request;
        $this->domain = env('APP_URL', '/');
        $this->filePath = filePath();
        $this->path = path();
    }

    /**
     *
     * 验证接口访问者身份
     *
     * @param $token
     * @return bool
     */
    protected function authGuest($token)
    {
        if ($token) {
            $this->user = PRedis::HGetAll('token:' . $token);
            if ($this->user) return true;
        }
        //abort(403, 'You have no access!');
        return false;
    }

    /**
     *
     * 验证接口访问者身份2
     *
     * @param $uid 用户ID
     * @param $token 用户Token
     * @return bool
     */
    protected function checkToken($uid, $token)
    {
        if (!empty($uid) && !empty($token)) {
            $this->user = PRedis::HGetAll('user:' . $uid);
            if (!empty($this->user['token']) && $this->user['token'] == $token) return true;
        }
        return false;
    }
}